package com.ruoyi.testfunction.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.testfunction.utils.ApiUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.testfunction.domain.FunctionType;
import com.ruoyi.testfunction.service.IFunctionTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 功能类型Controller
 *
 * @author LF
 * @date 2024-03-06
 */
@RestController
@RequestMapping("/testfunction/type")
public class FunctionTypeController extends BaseController {
    @Autowired
    private IFunctionTypeService functionTypeService;

    /**
     * 笑话API测试
     *
     * @param quantity 数量
     * @return String
     * @throws IOException
     */
    @GetMapping("/jokeTest/{quantity}")
    public String jokeTest(@PathVariable("quantity") int quantity) throws IOException {
        return ApiUtil.JokeAPI(quantity);
    }

    /**
     * 天气预报API测试
     *
     * @param code 城市编码
     * @return String
     * @throws IOException
     */
    @GetMapping("/weatherTest/{code}")
    public String weatherTest(@PathVariable("code") String code) throws IOException {
        return ApiUtil.WeatherPlus(code);
    }

    /**
     * 查询功能类型列表
     */
    @PreAuthorize("@ss.hasPermi('testfunction:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(FunctionType functionType) {
        startPage();
        List<FunctionType> list = functionTypeService.selectFunctionTypeList(functionType);
        return getDataTable(list);
    }

    /**
     * 导出功能类型列表
     */
    @PreAuthorize("@ss.hasPermi('testfunction:type:export')")
    @Log(title = "功能类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FunctionType functionType) {
        List<FunctionType> list = functionTypeService.selectFunctionTypeList(functionType);
        ExcelUtil<FunctionType> util = new ExcelUtil<FunctionType>(FunctionType.class);
        util.exportExcel(response, list, "功能类型数据");
    }

    /**
     * 获取功能类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('testfunction:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(functionTypeService.selectFunctionTypeById(id));
    }

    /**
     * 新增功能类型
     */
    @PreAuthorize("@ss.hasPermi('testfunction:type:add')")
    @Log(title = "功能类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FunctionType functionType) {
        return toAjax(functionTypeService.insertFunctionType(functionType));
    }

    /**
     * 修改功能类型
     */
    @PreAuthorize("@ss.hasPermi('testfunction:type:edit')")
    @Log(title = "功能类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FunctionType functionType) {
        return toAjax(functionTypeService.updateFunctionType(functionType));
    }

    /**
     * 删除功能类型
     */
    @PreAuthorize("@ss.hasPermi('testfunction:type:remove')")
    @Log(title = "功能类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(functionTypeService.deleteFunctionTypeByIds(ids));
    }
}
